<?php

/**
 *    我的收货地址控制器
 *
 *    @author liangcajian QQ632324010
 *    @usage    none
 */
class My_addressApp extends MemberbaseApp
{
    function index()
    {
        /* 搜索条件 */
        $conditions = "user_id = " . $this->visitor->get('user_id');
        if (isset($_GET['type']) && htmldecode($_GET['type'])){
            $address_curmenu = intval($_GET['type'])==1 ? "sj_address" : "zj_address" ;
			$conditions .= " AND ddl_type = " . intval($_GET['type']);
        }else{
			$address_curmenu = "address_list";
		}
		/* 取得列表数据 */
        $model_address =& m('address');
        $addresses     = $model_address->find(array(
            'conditions'    => $conditions,
			'order'      => 'case when default_use>0 then 0 else 1 end, default_use DESC, addr_id DESC',
        ));
        $this->assign('addresses', $addresses);

        /* 当前位置 */
        $this->_curlocal(LANG::get('member_center'),    'index.php?app=member',
                         LANG::get('my_address'), 'index.php?app=my_address',
                         LANG::get('address_list'));

        /* 当前用户中心菜单 */
        $this->_curitem('my_address');
        /* 当前所处子菜单 */
        $this->_curmenu($address_curmenu);

        $this->import_resource(array(
            'script' => array(
                array('path' => 'dialog/dialog.js', 'attr' => 'id="dialog_js"',),
                array('path' => 'jquery.ui/jquery.ui.js', 'attr' => '',),
                array('path' => 'jquery.ui/i18n/' . i18n_code() . '.js', 'attr' => '',),
                array('path' => 'jquery.plugins/jquery.validate.js',  'attr' => '',),
                array('path' => 'mlselection.js', 'attr' => '',),				
            ),
            'style' =>  'jquery.ui/themes/ui-lightness/jquery.ui.css',
        ));	
        $this->_config_seo('title', Lang::get('member_center') . ' - ' . Lang::get('my_address'));
        $this->display('my_address.index.html');
    }

    /**
     *    添加地址
     *
     *    @author liangcajian QQ632324010
     *    @return    void
     */
    function add()
    {
        if (!IS_POST)
        {
            $this->assign('act', 'add');
            //$this->_get_regions();
			
			/* 当前用户中心菜单 */
			$this->_curitem('my_address');
			/* 当前所处子菜单 */
			$this->_curmenu('add_address');		
			$this->_config_seo('title', Lang::get('member_center') . ' - ' . Lang::get('add_address'));
			$this->import_resource('mlselection.js, jquery.plugins/jquery.validate.js');	
            $this->display('my_address.form.html');
        }
        else
        {
            /* 电话和手机至少填一项 */
            if (!$_POST['phone_tel'] && !$_POST['phone_mob'])
            {
                $this->pop_warning('phone_required');

                return;
            }

            $region_name = $_POST['region_name'];
            $data = array(
                'user_id'       => $this->visitor->get('user_id'),
                'ddl_type'      => htmldecode($_POST['ddl_type']),
				'consignee'     => htmldecode($_POST['consignee']),
                'region_id'     => htmldecode($_POST['region_id']),
                'region_name'   => htmldecode($_POST['region_name']),
                'address'       => htmldecode($_POST['address']),
                'zipcode'       => htmldecode($_POST['zipcode']),
                'phone_tel'     => htmldecode($_POST['phone_tel']),
                'phone_mob'     => htmldecode($_POST['phone_mob']),
				'txt_countrys'  => htmldecode($_POST['txt_country']).'//'.htmldecode($_POST['txt_province']).'//'.htmldecode($_POST['txt_city']).'//'.htmldecode($_POST['txtqu']),//国家/地区/省/市,
				'idpic'         => htmldecode($_POST['idpic']),
				'txtidcode'     => htmldecode($_POST['txtidcode']),
				'txt_email'     => htmldecode($_POST['txt_email']),
				'txt_company'   => htmldecode($_POST['txt_company']),
				'addtime'   => time(),
            );
			//开始合并身份证正面和反面两张图片，合成一张
			$id_photo = $this->hebing_photo($_POST['c_input'],$_POST['c_input2']);
			$id_photo && $data['id_photo'] = $id_photo;
						
            $model_address =& m('address');
            if (!($address_id = $model_address->add($data)))
            {
                $this->show_warning($model_address->get_error());

                return;
            }
			$this->write_log($this->visitor->get('user_id'), $this->visitor->get('user_name'), '添加地址,ID:'.$address_id, 0);//添加操作日志	
			
            $this->show_message('添加成功',
				'back_list',    'index.php?app=my_address',
				'continue_add', 'index.php?app=my_address&amp;act=add'
             );				
            //$this->pop_warning('ok', APP.'_'.ACT);
        }
    }
    function edit()
    {
        $addr_id = empty($_GET['addr_id']) ? 0 : intval($_GET['addr_id']);
        if (!$addr_id)
        {
			$this->show_warning('no_such_address');
            return;
        }
        if (!IS_POST)
        {
            $model_address =& m('address');
            $find_data     = $model_address->find("addr_id = {$addr_id} AND user_id=" . $this->visitor->get('user_id'));
            if (empty($find_data))
            {
                $this->show_warning('no_such_address');
                return;
            }
            $address = current($find_data);

			/* 当前用户中心菜单 */
			$this->_curitem('my_address');
			/* 当前所处子菜单 */
			$this->_curmenu('edit_address');

			//四、填写发货地址 五、填写收货地址
			$countrys1 = explode('//',$address['txt_countrys']);
			$address['txt_country'] = $countrys1[0];
			$address['txt_province'] = $countrys1[1];
			$address['txt_city'] = $countrys1[2];
			$address['txtqu'] = $countrys1[3];
			
		    $this->assign('address', $address);
            $this->import_resource('mlselection.js, jquery.plugins/jquery.validate.js');
            $this->assign('act', 'edit');
            //$this->_get_regions();
			$this->_config_seo('title', Lang::get('member_center') . ' - ' . Lang::get('edit_address'));
            $this->display('my_address.form.html');
        }
        else
        {
            /* 电话和手机至少填一项 */
            if (!$_POST['phone_tel'] && !$_POST['phone_mob'])
            {
				$this->show_warning('phone_required');
                return;
            }

            $data = array(
                'ddl_type'      => htmldecode($_POST['ddl_type']),
				'consignee'     => htmldecode($_POST['consignee']),
                'region_id'     => htmldecode($_POST['region_id']),
                'region_name'   => htmldecode($_POST['region_name']),
                'address'       => htmldecode($_POST['address']),
                'zipcode'       => htmldecode($_POST['zipcode']),
                'phone_tel'     => htmldecode($_POST['phone_tel']),
                'phone_mob'     => htmldecode($_POST['phone_mob']),
				'txt_countrys'  => htmldecode($_POST['txt_country']).'//'.htmldecode($_POST['txt_province']).'//'.htmldecode($_POST['txt_city']).'//'.htmldecode($_POST['txtqu']),//国家/地区/省/市,
				'idpic'         => htmldecode($_POST['idpic']),
				'txtidcode'     => htmldecode($_POST['txtidcode']),
				'txt_email'     => htmldecode($_POST['txt_email']),
				'txt_company'   => htmldecode($_POST['txt_company']),
            );
			//开始合并身份证正面和反面两张图片，合成一张
			$id_photo = $this->hebing_photo($_POST['c_input'],$_POST['c_input2']);
			$id_photo && $data['id_photo'] = $id_photo;
						
            $model_address =& m('address');
            $model_address->edit("addr_id = {$addr_id} AND user_id=" . $this->visitor->get('user_id'), $data);
            if ($model_address->has_error())
            {
                $this->show_warning($model_address->get_error());
                return;
            }
			$this->write_log($this->visitor->get('user_id'), $this->visitor->get('user_name'), '修改地址,ID:'.$addr_id, 0);//添加操作日志
            $this->show_message('编辑成功',
				'back_list',    'index.php?app=my_address',
				'重新修改', 'index.php?app=my_address&amp;act=edit&amp;addr_id='.$addr_id
             );					
            //$this->pop_warning('ok', APP.'_'.ACT);
        }
    }
    function drop()
    {
        $addr_id = isset($_GET['addr_id']) ? htmldecode($_GET['addr_id']) : 0;
        if (!$addr_id)
        {
            $this->show_warning('no_such_address');

            return;
        }
        $ids = explode(',', $addr_id);//获取一个类似array(1, 2, 3)的数组
        $model_address  =& m('address');
        $drop_count = $model_address->drop("user_id = " . $this->visitor->get('user_id') . " AND addr_id " . db_create_in($ids));
        if (!$drop_count)
        {
            /* 没有可删除的项 */
            $this->show_warning('no_such_address');

            return;
        }

        if ($model_address->has_error())    //出错了
        {
            $this->show_warning($model_address->get_error());

            return;
        }
		$this->write_log($this->visitor->get('user_id'), $this->visitor->get('user_name'), '删除地址,ID:'.$addr_id, 0);//添加操作日志

        $this->show_message('drop_address_successed');
    }
	
	//设为默认地址
	function set()
	{
        $addr_id = isset($_GET['addr_id']) ? htmldecode($_GET['addr_id']) : 0;
        if (!$addr_id)
        {
            $this->show_warning('没有该地址');
            return;
        }
		$model_address =& m('address');
		$model_address->edit("user_id=" . $this->visitor->get('user_id'), "default_use = 0");
        $model_address->edit("addr_id = {$addr_id} AND user_id=" . $this->visitor->get('user_id'), "default_use = 1");

		$this->write_log($this->visitor->get('user_id'), $this->visitor->get('user_name'), '地址设为默认地址成功,ID:'.$addr_id, 0);//添加操作日志
		header('location:index.php?app=my_address');
	}
		
    function _get_regions()
    {
        $model_region =& m('region');
        $regions = $model_region->get_list(1);
        if ($regions)
        {
            $tmp  = array();
            foreach ($regions as $key => $value)
            {
                $tmp[$key] = $value['region_name'];
            }
            $regions = $tmp;
        }
        $this->assign('regions', $regions);
    }
	//开始合并身份证正面和反面两张图片，合成一张
	function hebing_photo($c_input, $c_input2)
	{
		$id_photo = '';
		if($c_input && $c_input2)
		{
			$imgs = array(); 
			$imgs[0] = str_replace('/data/','data/',$c_input);
			$imgs[1] = str_replace('/data/','data/',$c_input2); 
			$target = 'data/files/mall/idphoto/emp.jpg';//背景图片 
			$target_img = Imagecreatefromjpeg($target); 
			$source= array();
			foreach ($imgs as $k=>$v){ 
				$source[$k]['source'] = Imagecreatefromjpeg($v); 
				$source[$k]['size'] = getimagesize($v); 
			} 
			$num1=0; 
			$num=0; //控制列数，一行几列，0为1以此类推。 
			$tmp=2; 
			$tmpy=5; //图片之间的间距 
			for ($i=0; $i<2; $i++){  
				imagecopy($target_img,$source[$i]['source'],$tmp,$tmpy,0,0,$source[$i]['size'][0],$source[$i]['size'][1]); 
				$tmp = $tmp+$source[$i]['size'][0]; 
				$tmp = $tmp+5; 
				if($i==$num){ 
					$tmpy = $tmpy+$source[$i]['size'][1]; 
					$tmpy = $tmpy+5; 
					$tmp=2; 
					$num=$num+3; 
				} 
			}
			$id_photo = date("YmdHis").'_'.floor(microtime() * 1000).'.jpg';
			Imagejpeg($target_img,'data/files/mall/idphoto/'.$id_photo); 
			unlink($imgs[0]);
			unlink($imgs[1]);
		}
		return $id_photo;
	}
    /**
     *    三级菜单
     *
     *    @author liangcajian QQ632324010
     *    @return    void
     */
    function _get_member_submenu()
    {
        $menus = array(
            array(
                'name'  => 'address_list',
                'url'   => 'index.php?app=my_address',
            ),
            array(
                'name'  => 'sj_address',
                'url'   => 'index.php?app=my_address&type=1',
            ),
            array(
                'name'  => 'zj_address',
                'url'   => 'index.php?app=my_address&type=2',
            ),						
            array(
                'name'  => 'add_address',
                'url'   => 'index.php?app=my_address&act=add',
            ),
        );
        if (ACT == 'edit')
        {
            $menus[] = array(
                'name' => 'edit_address',
                'url'  => '',
            );
        }
        return $menus;
    }
}

?>